package com.freshcity.page.common
{
	import com.freshcity.common.UserDetailWin;
	import com.freshcity.component.alert.Alert;
	import com.freshcity.component.container.Component;
	import com.freshcity.component.container.Window;
	import com.freshcity.core.Dispatcher;
	import com.freshcity.core.GlobalVar;
	import com.freshcity.core.MainScene;
	import com.freshcity.core.OpCodes;
	import com.freshcity.lib.event.DataEvent;
	import com.freshcity.lib.event.EventName;
	import com.freshcity.lib.tooltip.ToolTipsManager;
	import com.freshcity.manager.EffectManager;
	import com.freshcity.manager.LayerManager;
	import com.freshcity.manager.MsgManager;
	import com.freshcity.page.common.UserInfoDetailPanel;
	import com.freshcity.page.friend.RequestFriend;
	import com.freshcity.page.talk.RequestTalk;
	import com.freshcity.page.user.GetQQInfo;
	import com.freshcity.utils.BitmapUtil;
	import com.freshcity.utils.GlobalUtil;
	import com.freshcity.utils.MessageUtil;
	import com.freshcity.utils.StringUtil;
	import com.greensock.TweenMax;
	import com.greensock.loading.ImageLoader;
	
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.text.StyleSheet;

	public class UserDetail extends Component
	{
		private var _home:UserHome;
		private var css:String="".concat("a{color: #0000ff;}", "a:hover{color:#ff0000;textDecoration: underline;}");

		private var back:UserDetailWin;
		private var imageLoader:ImageLoader;
		private var user:Object;
		private var report:Object;
		private var _viewModel:Boolean;

		public var funModel:int;

		public function UserDetail(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0)
		{
			super(parent, xpos, ypos);
			this.mouseEnabled=false;
			this.visible=false;
			this.setSize(500, 300);

			back.chatBtn.addEventListener(MouseEvent.CLICK, onReqChat);
			back.waterBtn.addEventListener(MouseEvent.CLICK,waterBtnHandler);
			back.wordsBtn.addEventListener(MouseEvent.CLICK, sendWord);
			back.qqBtn.addEventListener(MouseEvent.CLICK, getQQ);
			back.followIcon.addEventListener(MouseEvent.CLICK,followIconHandler);
			Dispatcher.addEventListener(EventName.ReqFriendDone,onReqFriendDoneHandler);
		}
		
		protected function followIconHandler(event:MouseEvent):void
		{
			//var timeDesc:String = "".concat("<font color=\'#ff0000\' size=\'13\'>",back.dateCombox.selectedLabel,"</font>");
			var htmlContent:String="  确认取消关注吗？";
			var htmlTitle:String="<font color=\'#f5ff00\'>解除关系</font>";
			
			Alert.show(htmlContent, htmlTitle, Alert.OK | Alert.CANCEL, null, requestRemoveAsso);
		}
		
		private function requestRemoveAsso(arg1:int):void
		{
			if (arg1 == Alert.OK)
			{
				main.commu.send(OpCodes.CANCEL_GUANZHU, {to: String(this.user.id)}, onRemoveAssoCallBack);
			}
		}
		
		private function onRemoveAssoCallBack(data:*):void
		{
			if (data == true)
			{
				main.msg.showRollTipsMsg("成功完成操作");
				back.followIcon.visible = false;
				main.friendPanel.removeFriend(this.user);
			}
			else
			{
				main.msg.showRollTipsMsg(data);
			}
		}
		
		private function onReqFriendDoneHandler(evt:DataEvent):void
		{
			var data:* = evt.data;
			if(data.id==this.user.id){
				back.followIcon.visible = true;
			}
		}
		
		protected function waterBtnHandler(event:MouseEvent):void
		{
			if(user.sex=='female'){
				home.forwardPage(2);
			}else{
				main.commu.send(187, {to: this.user.id}, onReqWaterCallBack);
			}
		}
		
		private function onReqWaterCallBack(data:*):void
		{
			if(data==true){
				main.msg.showRollTipsMsg("信息已发出");
				back.waterBtn.enabled=false;
			}else{
				var htmlContent:String="没有发现你的梧桐树哦。栽下梧桐树，找到真心人，你要立刻去栽树吗？";
				Alert.show(htmlContent, "", Alert.OK | Alert.CANCEL, null, forwardNewTree);
			}
		}
		
		private function forwardNewTree(arg1:int):void
		{
			if (arg1 == Alert.OK)
			{
				home.visible=false;
				main.mainNav.onShowTree();
			}
		}

		protected function getQQ(event:MouseEvent):void
		{
			if (main.user.level < 3)
			{
				showLevelUpTip();
			}
			else if (main.user.qqNo == '')
			{
				main.getQQInfo(reqQQNo);
			}
			else
			{
				reqQQNo();
			}
		}

		private function showLevelUpTip():void
		{
			var levelStr:String=StringUtil.addColor(this.user.level, "#ff0000");
			var s2:String="".concat("你的个人信用等级太低了<br/>", "<p align='right'><a href='event:'>", ">>立即提升信用", "</a></p>");
			main.msg.showPopUpMsg(s2, MsgManager.HELLO, 0, MsgManager.CENTER, main.statusPanel.showUpgrade);
		}
		
		private function showSendFlowerTip():void
		{
			if (GlobalUtil.dictLen(main.friends) >= GlobalVar.MAX_FRIEND_AMT)
			{
				main.msg.showRollTipsMsg("意中人总数不能超过" + GlobalVar.MAX_FRIEND_AMT + "个");
			}else{
				var rf:RequestFriend = new RequestFriend(this.user.id);
				rf.main = this.main;
				rf.show();
			}
		}		

		private function reqQQNo():void
		{
			back.qqBtn.enabled=false;
			main.commu.send(OpCodes.REQ_QQ_NO, {u: user.id}, onGetQQCallBack);
		}

		private function onGetQQCallBack(data:*):void
		{
			back.qqBtn.enabled=true;
			if (data is String)
			{
				var qi:GetQQInfo=new GetQQInfo(LayerManager.alertLayer);
				qi.main=main;
				qi.qqNo=data;
				qi.show();
			}
			else
			{
				main.msg.showRollTipsMsg(data.msg);
			}
		}

		override protected function addChildren():void
		{
			super.addChildren();
			back=new UserDetailWin();
			addChild(back);
			
			back.followIcon.toolTipData="已关注，点击可取消关注";
			ToolTipsManager.register(back.followIcon);	
		}

		override public function draw():void
		{
			super.draw();
		}

		private function onlineChatValidate():void
		{
			if (user.sex != GlobalVar.LOGIN_USER.sex)
			{
				main.commu.send(185, {u: user.id}, onlineCallback);
			}
		}

		private function onlineCallback(data:*):void
		{
			if (data == true)
			{
				back.chatBtn.enabled=true;
			}
		}

		private function onReqChat(event:Event):void
		{
			new RequestTalk(this, this.user.id);
		}

		private function sendWord(event:Event):void
		{
			if (main.friends[user.id] !=null && main.friends[user.id].level>0)
			{
				MainScene.writeMsg.uid=user.id;
				MainScene.writeMsg.isReply=false;
				MainScene.writeMsg.onShow();
			}
			else
			{
				showSendFlowerTip();
			}
		}

		public function get viewModel():Boolean
		{
			return _viewModel;
		}

		public function set viewModel(value:Boolean):void
		{
			_viewModel=value;
		}

		public function get home():UserHome
		{
			return _home;
		}

		public function set home(value:UserHome):void
		{
			_home=value;
		}

		override public function show(event:Event=null):void
		{
			this.user=home.data.user;
			this.report=home.data.report;

			back.wordsBtn.enabled=!this.viewModel;
			back.chatBtn.enabled=false;
			//back.qqBtn.enabled=(user.qqNo != '');
			back.qqBtn.mouseEnabled=(user.qqNo != '');
			back.wordsBtn.enabled=(user.sex != GlobalVar.LOGIN_USER.sex);
			back.userPic.gotoAndStop(user.sex);
			back.wordsBtn.label = "求认识";
			back.waterBtn.enabled = true;
			back.waterBtn.label = (user.sex=='female')?"赠清水":"求清水";
			BitmapUtil.showAvatarPic(user, back.userPic);

			var propertyBox:UserInfoDetailPanel=new UserInfoDetailPanel(user, report);
			propertyBox.main=main;
			back.infoPanel.focusEnabled=false;
			back.infoPanel.source=propertyBox;
			back.infoPanel.update();
			back.level.text=MessageUtil.decodeLevel(user.level);
			back.idNo.text='用户' + user.id;

			back.followIcon.visible=(main.friends[user.id]!=null);
			onlineChatValidate();
			validateBtnVisible();
			super.show();
		}
		
		private function validateBtnVisible():void
		{
			 var flag:Boolean = main.user.sex != user.sex;
			back.wordsBtn.visible=flag;
			back.waterBtn.visible=flag;
			back.chatBtn.visible=flag;
			back.qqBtn.visible=flag;
		}
	}
}
